package com.example.cardgame

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.RadioButton
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.cardgame.databinding.ActivitySecondBinding
import java.io.ByteArrayOutputStream

class SecondActivity:ComponentActivity() {
    private  val binding by lazy {
        ActivitySecondBinding.inflate(layoutInflater)
    }
    private lateinit var hobbies: MutableList<String>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        //调用相机并保存照片
        val launcher = registerForActivityResult(ActivityResultContracts.TakePicturePreview()){
            binding.picture.setImageBitmap(it)
        }
        binding.takepicture.setOnClickListener {
            launcher.launch(null)
        }

        //将信息页面数据传到修改页面上
        hobbies= mutableListOf()
        val intent = getIntent()
        var sexcheck = 0
        binding.editname.setText(intent.getStringExtra(NAME_MSG))
        binding.editsno.setText(intent.getStringExtra(SNO_MSG))
        binding.edittel.setText(intent.getStringExtra(TEL_MSG))
        binding.editemail.setText(intent.getStringExtra(EMAIL_MSG))
        //将性别信息传到radiobutton上
        if(intent.getStringExtra(SEX_MSG)==binding.radioman.text.toString()){
            binding.radioman.isChecked=true
        }
        else if(intent.getStringExtra(SEX_MSG)==binding.radiowoman.text.toString()){
            binding.radiowoman.isChecked=true
        }
        //将爱好信息传到checkbox上
        intent.getStringArrayListExtra(HOBBY_MSG)?.let {
            binding.checkBoxPlay.isChecked=it.contains(binding.checkBoxPlay.text.toString())
            binding.checkBoxTravel.isChecked=it.contains(binding.checkBoxTravel.text.toString())
            binding.checkBoxRead.isChecked=it.contains(binding.checkBoxRead.text.toString())
            binding.checkBoxShop.isChecked=it.contains(binding.checkBoxShop.text.toString())
        }
        //照片传送
        val bitmapBytes = intent.getByteArrayExtra(IMG_MSG)
        val bitmap = bitmapBytes?.let { BitmapFactory.decodeByteArray(bitmapBytes, 0, it.size) }
        binding.picture.setImageBitmap(bitmap)

        //保存按钮的监听
        binding.keep.setOnClickListener{
            val intent = Intent()
            val extras = Bundle()
            val checkedButton=findViewById<RadioButton>(binding.radiosex.checkedRadioButtonId)
            extras.putString(NAME_MSG,binding.editname.text.toString())
            extras.putString(SEX_MSG,checkedButton.text.toString())
            extras.putString(SNO_MSG,binding.editsno.text.toString())
            extras.putString(TEL_MSG,binding.edittel.text.toString())
            extras.putString(EMAIL_MSG,binding.editemail.text.toString())
            //爱好信息从checkbox转成数组形式
            if(binding.checkBoxPlay.isChecked) hobbies.add(binding.checkBoxPlay.text.toString())
            if(binding.checkBoxTravel.isChecked) hobbies.add(binding.checkBoxTravel.text.toString())
            if(binding.checkBoxRead.isChecked) hobbies.add(binding.checkBoxRead.text.toString())
            if(binding.checkBoxShop.isChecked) hobbies.add(binding.checkBoxShop.text.toString())
            extras.putStringArrayList(HOBBY_MSG,hobbies as ArrayList<String>)
            //照片传递
            if (binding.picture.drawable != null) {
                val bitmap = binding.picture.drawable.toBitmap()
                // 将Bitmap转换为byte[]数组
                val byteArrayOutputStream = ByteArrayOutputStream()
                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream)
                val bitmapBytes = byteArrayOutputStream.toByteArray()
                extras.putByteArray(IMG_MSG,bitmapBytes)
            }

            intent.putExtras(extras)
            setResult(1,intent)
            finish()

        }
        //取消按钮
        binding.cancel.setOnClickListener {
            finish()
        }
    }
}